package v3.chapter5.timerTest5;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Run {
	
	private static Timer timer=new Timer();
	private static int runCount=0;
	
	static public class MyTask1 extends TimerTask{ 
		@Override
		public void run() { 
			try {
				System.out.println("1 begin 运行了！时间为：" + new Date().toLocaleString());
				Thread.sleep(1000);
				System.out.println("1   end 运行了！时间为：" + new Date().toLocaleString());
				runCount++;
				if(runCount==5)
					timer.cancel();
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
		} 
	}

	public static void main(String[] args) {
		try {
			MyTask1 task1=new MyTask1();
			SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString1 = "2019-04-22 17:10:20";
			Date dateRef1=sdf1.parse(dateString1);
			System.out.println("字符串1时间：" + dateRef1.toLocaleString() + " 当前时间："+ new Date().toLocaleString());
			timer.schedule(task1, dateRef1,3000);
		}catch(ParseException e) {
			e.printStackTrace();
		}
	}
}
